package com.office.automate.servlet.employee;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.exception.general.GeneralException;
import com.office.automate.model.authenticate.UserAuthenticator;
import com.office.automate.model.employee.Employee;
import com.office.automate.service.def.employee.EmployeeService;
import com.office.automate.service.impl.employee.EmployeeServiceImplementer;

/**
 * Servlet implementation class UploadPhotoServlet
 */
@WebServlet("/UploadPhotoServlet")
public class UploadPhotoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadPhotoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("rawtypes")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/HomePage.jsp");
		response.setContentType("text/html;charset=UTF-8");
        
        try {
            // Apache Commons-Fileupload library classes
        	EmployeeService employeeRegistrationService = new EmployeeServiceImplementer();
        	UserAuthenticator currentUser = (UserAuthenticator) (request.getSession().getAttribute("currentSessionUser"));
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload sfu  = new ServletFileUpload(factory);
            if (! ServletFileUpload.isMultipartContent(request)) {
                System.out.println("sorry. No file uploaded");
                return;
            }
            List items = sfu.parseRequest(request);
            FileItem file = (FileItem) items.get(0);
            Employee updateEmployee = new Employee();
            updateEmployee.setEmployeeId(currentUser.getEmployeeId());
            updateEmployee.setImage(file.get());
            employeeRegistrationService.uploadProfileImage(updateEmployee);
            dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/UploadImage.jsp");
            dispatcher.forward(request, response);
		} catch (Exception e) {
			FrontierVisibleException exception = new FrontierVisibleException(GeneralException.SYSTEM_ERROR, e);
			request.setAttribute("OperationalError", exception);
			dispatcher.forward(request, response);
		}
	}
}
